package com.lgkj.ui.main;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lgkj.R;
import com.lgkj.ui.ImageBrowseActivity;
import com.lgkj.ui.LoginActivity;
import com.lgkj.ui.OptionActivity;
import com.lgkj.ui.UserInfoActivity;
import com.lgkj.ui.XieYiActivity;
import com.lgkj.ui.manager.ShopManagerActivity;
import com.lgkj.ui.manager.ShopManagerNullActivity;
import com.lgkj.ui.userinfo.AdressActivity;
import com.lgkj.ui.userinfo.CollectionActivity;
import com.lgkj.ui.userinfo.CommentActivity;
import com.lgkj.ui.userinfo.ExtendActivity;
import com.lgkj.ui.userinfo.OrderActivity;
import com.lgkj.ui.userinfo.TeamActivity;
import com.lgkj.ui.userinfo.TiXianActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.SharedPreferencesUtils;
import com.meiqia.meiqiasdk.imageloader.MQImage;
import com.meiqia.meiqiasdk.util.MQIntentBuilder;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/19 0019.
 */

public class MineFragment extends Fragment {
    private static final String TAG = "MineFragment";
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_adress)
    TextView tvAdress;
    @BindView(R.id.tv_shop_namager)
    TextView tvShopNamager;
    @BindView(R.id.tv_user_info)
    TextView tvUserInfo;
    @BindView(R.id.tv_opinion)
    TextView tvOpinion;
    @BindView(R.id.tv_about)
    TextView tvAbout;
    @BindView(R.id.tv_tuichu)
    TextView tvTuichu;
    Unbinder unbinder;
    @BindView(R.id.tv_set_position)
    TextView tvSetPosition;
    @BindView(R.id.tv_card)
    TextView tvCard;
    private View view;
    String loadpath, url, level;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.main_mine_fragment, null);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        level = SharedPreferencesUtils.getParam(getActivity(), "level", "").toString();
        Log.i(TAG, "onResume: " + level);
        init();
        String shenfen = "";
        if (level.equals("1")) {
            shenfen = "会员";
        } else if (level.equals("2")) {
            shenfen = "创客身份";
        } else if (level.equals("3")) {
            shenfen = "合伙人身份";
        } else if (level.equals("4")) {
            shenfen = "城市代理身份";
        } else {
            shenfen = "普通";
        }
        tvCard.setText(shenfen);
    }

    private void init() {
        tvAdress.setText(SharedPreferencesUtils.getParam(getActivity(), "adress", "").toString());
        tvName.setText(SharedPreferencesUtils.getParam(getActivity(), "nickname", "").toString());
        loadpath = SharedPreferencesUtils.getParam(getActivity(), "loadheadimg", "").toString();
        if (loadpath != null && loadpath.length() > 0) {
            CommonUtils.loadImageRound(getActivity(), ivHead, new File(loadpath), 0);
        } else {
            url = SharedPreferencesUtils.getParam(getActivity(), "headimg", "").toString();
            CommonUtils.loadImageRound(getActivity(), ivHead, url, 0);
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_shop_namager, R.id.tv_service, R.id.tv_user_info, R.id.tv_opinion, R.id.tv_about, R.id.tv_tuichu, R.id.iv_head, R.id.tv_set_position, R.id.tv_order, R.id.ll_card, R.id.tv_extend, R.id.tv_income, R.id.tv_team, R.id.tv_comment, R.id.tv_collection, R.id.tv_adress_sh})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_shop_namager:
                String shop_id = SharedPreferencesUtils.getParam(getActivity(), "shop_id", "").toString();
                Log.i("MineFragment", "successHttp: " + shop_id);
                if (shop_id != null && !shop_id.equals("null")) {
                    startActivity(new Intent(getActivity(), ShopManagerActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), ShopManagerNullActivity.class));
                }
                break;
            case R.id.tv_user_info:
                startActivity(new Intent(getActivity(), UserInfoActivity.class));
                break;
            case R.id.tv_service:
                showkefu();
//                MQImage.setImageLoader(new com.lgkj.utils.MQGlideImageLoader());
//                Intent serviceIntent = new MQIntentBuilder(getActivity()).build();
//                startActivity(serviceIntent);
                break;
            case R.id.tv_opinion:
                startActivity(new Intent(getActivity(), OptionActivity.class));
                break;
            case R.id.tv_set_position:
                cleanPositon();
                break;
            case R.id.tv_about:
//                Intent intent = new Intent(getActivity(), WebViewActivity.class);
//                intent.putExtra("title", "关于我们");
//                intent.putExtra("url", "http://www.lianguangtech.com/lg/about/index.html");
//                startActivity(intent);
                Intent intent = new Intent(getActivity(), XieYiActivity.class);
                intent.putExtra("xieyi", false);
                startActivity(intent);
                break;
            case R.id.tv_tuichu:
                SharedPreferencesUtils.setParam(getActivity(), "phone", "");
                SharedPreferencesUtils.setParam(getActivity(), "is_login", "1");
                startActivity(new Intent(getActivity(), LoginActivity.class));
                getActivity().finish();
                break;
            case R.id.iv_head:
                Intent imgIntent = new Intent(getActivity(), ImageBrowseActivity.class);
                if (loadpath != null && loadpath.length() > 0) {
                    imgIntent.putExtra("image_url", loadpath);
                    imgIntent.putExtra("loadpath", true);
                } else {
                    imgIntent.putExtra("image_url", url);
                }
//                startActivity(imgIntent, ActivityOptions.makeSceneTransitionAnimation(getActivity(), ivHead, "shareNames").toBundle());
                break;
            case R.id.tv_order:
                startActivity(new Intent(getActivity(), OrderActivity.class));
                break;
            case R.id.ll_card:
//                ApplyCard();
                startActivity(new Intent(getActivity(), com.lgkj.ui.CardActivity.class));
                break;
            case R.id.tv_extend:
                startActivity(new Intent(getActivity(), ExtendActivity.class));
                break;
            case R.id.tv_income:
                if (Integer.parseInt(level) < 2) {
                    Toast.makeText(getActivity(), "你还有没有团队人员，暂无收益", Toast.LENGTH_SHORT).show();
                } else {
                    startActivity(new Intent(getActivity(), TiXianActivity.class));
                }
                break;
            case R.id.tv_team:
                if (Integer.parseInt(level) < 2) {
                    Toast.makeText(getActivity(), "你还有没有团队人员", Toast.LENGTH_SHORT).show();
                } else {
                    startActivity(new Intent(getActivity(), TeamActivity.class));
                }
                break;
            case R.id.tv_comment:
                startActivity(new Intent(getActivity(), CommentActivity.class));
                break;
            case R.id.tv_collection:
                startActivity(new Intent(getActivity(), CollectionActivity.class));
                break;
            case R.id.tv_adress_sh:
                startActivity(new Intent(getActivity(), AdressActivity.class));
                break;
        }
    }

    private boolean code = true;

    private void cleanPositon() {
        if (code) {
            code = false;
            tvSetPosition.setText("不显示定位信息");
        } else {
            code = true;
            tvSetPosition.setText("显示定位信息");
        }
        CommonUtils.showDiaLog(getActivity(), "处理中");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(getActivity()));
        HttpUtils.postHttp(map, CommonConstant.cleanPositon, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                CommonUtils.dismissDiaLog();
            }

            @Override
            public void errorHttp(String msg) {
                CommonUtils.dismissDiaLog();
            }
        });

    }

    private void showkefu() {
        AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
                .setTitle("客服")//标题
                .setMessage("选择服务方式")//内容
                .setPositiveButton("电话", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent1 = new Intent(Intent.ACTION_CALL);
                        Uri data = Uri.parse("tel:0816-298-8116");
                        intent1.setData(data);
                        startActivity(intent1);
                    }
                })
                .setNeutralButton("客服", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MQImage.setImageLoader(new com.lgkj.utils.MQGlideImageLoader());
                        Intent serviceIntent = new MQIntentBuilder(getActivity()).build();
                        startActivity(serviceIntent);
                    }
                })
                .create();
        alertDialog.show();

    }
}